package material.command.undo;

public class RemoteControlWithUndo {
    Command[] onCommands;
    Command[] offCommands;
    Command undoCommand;//用于记录上一个操作的命令

    private final Integer commandNums = 7;

    public RemoteControlWithUndo() {
        onCommands = new Command[commandNums];
        offCommands = new Command[commandNums];
        Command noCommand = new NoCommand();
        for (int i = 0; i < commandNums; i++) {
            onCommands[i] = noCommand;
            offCommands[i] = noCommand;
        }
        undoCommand = noCommand;//一开始，并没有任何前一个命令，所以设置NoCommand
    }

    public void setCommand(int slot, Command onCommand, Command offCommand) {
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }

    /**
     * 当控下控钮,我们取得这个命令.并优先执行它，然后将它记录在undoCommand实例变量中。
     * 不管是是“开”或“关”命合,我们的处理方法都是一样的。
     *
     * @param solt
     */
    public void onButtonWasPushed(int solt) {
        onCommands[solt].execute();
        undoCommand = onCommands[solt];
    }

    public void offButtonWasPushed(int solt) {
        offCommands[solt].execute();
        undoCommand = offCommands[solt];
    }

    /**
     * 当控下撤销按钮时﹒我们调用undoCoamand实例变量的undo()方法．就可以倒转前一个命令。
     */
    public void undoButtonWasPushed() {
        undoCommand.undo();
    }
}
